package com.ews.onlineexchange.controller;

import java.util.List;

import javax.enterprise.inject.Model;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.xml.crypto.Data;

import com.ews.onlineexchange.model.Article;
import com.ews.onlineexchange.model.Category;
import com.ews.onlineexchange.model.DataEntity;
import com.ews.onlineexchange.service.EntityQuery;

@Model
public class ArticleList extends EntityQuery<Article> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected List<Predicate> getRestrictions(CriteriaBuilder cb, Root<Article> entity) {		
		List<Predicate> restrictions = super.getRestrictions(cb, entity);
		Join<Article, Category> category = entity.join("category");
		restrictions.add(cb.equal(category.get("id"), getParameterValues().get("categoryId")));
//		restrictions.add(cb.equal(entity.get("stat"), "1"));
		restrictions.add(cb.equal(entity.get("delFlag"), DataEntity.DEL_FLAG_NORMAL));
		return restrictions;
	}

	public ArticleList() {
		setMaxResults(MAX_RESULTS_NORMAL);
		setOrder("weight DESC ,sort DESC, createDate DESC,id");
	}

}
